import { Component, OnDestroy, OnInit } from '@angular/core';
import { delay } from 'rxjs/operators';
import { PermitConfig } from '../../../models';
import { BaseFieldComponent } from '../base-field/base-field.component';

@Component({
  selector: 'zepride-rmb-capitalize',
  templateUrl: './rmb-capitalize.component.html',
  styleUrls: ['./rmb-capitalize.component.less', '../field.common.less']
})
export class RmbCapitalizeComponent extends BaseFieldComponent implements OnInit, OnDestroy {

  displayValue: string;
  refTplComponent;

  permitConfig: PermitConfig[] = [
    { mode: 'query', permitId: '查看', permitName: '', authObjectId: '' }
  ];

  ngOnInit() {
    if (this.componentData.defaultValue) {
      this.displayValue = this.numberToUpper(this.componentData.defaultValue);
    }

    this.refTplComponent = this.componentData.extends.refTplComponent;

    if (this.refTplComponent) {
      const value = this.dataModel[this.refTplComponent.component.bindField];
      if (value) {
        this.displayValue = this.numberToUpper(value);
      }
    }

    this.modelChange$ = this.engineService.dataModelChange$.subscribe(data => {
      if (data.identifyId !== this.componentData.identifyId && this.refTplComponent) {
        if (data.tmpComItemId == this.refTplComponent.tplComponentTempId) {
          this.displayValue = this.numberToUpper(data.value);
          this.setData(this.displayValue);
        }
      }
    });
  }

  numberToUpper(money: any) {
    const cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); // 汉字的数字
    const cnIntRadice = new Array('', '拾', '佰', '仟'); // 基本单位
    const cnIntUnits = new Array('', '万', '亿', '兆'); // 对应整数部分扩展单位
    const cnDecUnits = new Array('角', '分', '毫', '厘'); // 对应小数部分单位
    const cnInteger = '整'; // 整数金额时后面跟的字符
    const cnIntLast = '元'; // 整型完以后的单位
    const maxNum = 999999999999999.9999; // 最大处理的数字
    let IntegerNum: string; // 金额整数部分
    let DecimalNum: string; // 金额小数部分
    let ChineseStr = ''; // 输出的中文金额字符串
    let parts: string[]; // 分离金额后用的数组，预定义
    if (money == '') {
      return '';
    }
    money = parseFloat(money);
    if (money >= maxNum) {
      alert('超出最大处理数字');
      return '';
    }
    if (money == 0) {
      ChineseStr = cnNums[0] + cnIntLast + cnInteger;
      return ChineseStr;
    }
    money = money.toString(); // 转换为字符串
    if (money.indexOf('.') == -1) {
      IntegerNum = money;
      DecimalNum = '';
    } else {
      parts = money.split('.');
      IntegerNum = parts[0];
      DecimalNum = parts[1].substr(0, 4);
    }
    if (parseInt(IntegerNum, 10) > 0) { // 获取整型部分转换
      let zeroCount = 0;
      const IntLen = IntegerNum.length;
      for (let i = 0; i < IntLen; i++) {
        const n = IntegerNum.substr(i, 1);
        const p = IntLen - i - 1;
        const q = p / 4;
        const m = p % 4;
        if (n == '0') {
          zeroCount++;
        } else {
          if (zeroCount > 0) {
            ChineseStr += cnNums[0];
          }
          zeroCount = 0; // 归零
          // tslint:disable-next-line: radix
          ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
        }
        if (m == 0 && zeroCount < 4) {
          ChineseStr += cnIntUnits[q];
        }
      }
      ChineseStr += cnIntLast;
      // 整型部分处理完毕
    }
    if (DecimalNum != '') { // 小数部分
      const decLen = DecimalNum.length;
      for (let i = 0; i < decLen; i++) {
        const n = DecimalNum.substr(i, 1);
        if (n != '0') {
          ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
        }
      }
    }
    if (ChineseStr == '') {
      ChineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (DecimalNum == '') {
      ChineseStr += cnInteger;
    }
    return ChineseStr;
  }

  ngOnDestroy() {
    if (this.modelChange$) {
      this.modelChange$.unsubscribe();
    }
  }
}
